import { Schema, model } from "mongoose";
import Types from "../typings";

// 定义 Schema 模型
const LifeSchema = new Schema<Types.Life.LifeType>({
  id: {
    type: String,
    required: true,
    unique: true,
  },

  title: {
    type: String,
    required: true,
    unique: true,
  },

  level: {
    type: String,
    required: true,
    default: "",
  },

  isFinish: {
    type: Boolean,
    required: true,
    default: false,
  },

  content: {
    type: String,
    required: true,
  },

  isTop: {
    type: Boolean,
    default: false,
  },

  createdTime: {
    type: Number,
    Number,
    required: true,
    default: Date.now(),
  },

  updatedTime: {
    type: Number,
    required: true,
    default: Date.now(),
  },

  creatorId: {
    type: String,
    required: true,
    default: "",
  },
});

// 排除 _id 和 __v 字段
LifeSchema.set("toObject", {
  virtuals: true,
  versionKey: false,
  transform: (doc, ret) => {
    delete ret._id;
    delete ret.__v;
  },
});

LifeSchema.set("toJSON", {
  virtuals: true,
  versionKey: false,
  transform: (doc, ret) => {
    delete ret._id;
    delete ret.__v;
  },
});

const LifeModel = model<Types.Life.LifeType>("Life", LifeSchema, "lifes");

export default LifeModel;
